package io.bluerain.exception;

import io.bluerain.converter.JsonUtil;

public class RpcException {

    public static String LACK_PARAMETERS = "Lack of key parameters.";

    public static void baseThrow(String message) {
        throw new RuntimeException(message);
    }

    public static String genBodyStruct(Result result) {
        return JsonUtil.writeJson(result);
    }

    /**
     * 自定义消息的404异常(结果未找到)
     *
     * @param message 消息内容
     */
    public static void throwNotFound(String message) {
        baseThrow(genBodyStruct(new Result(404, message)));
    }

    public static void throwNotFound() {
        throwNotFound(null);
    }

    /**
     * 自定义消息以及附带参数的参数错误
     *
     * @param message    消息
     * @param attachData 附带内容
     */
    public static void throwParamError(String message, Object attachData) {
        baseThrow(genBodyStruct(new Result(412, message, null, attachData)));
    }

    public static void throwParamError(String message) {
        throwParamError(message, null);
    }

    public static void throwParamError() {
        throwParamError(null);
    }

    /**
     * 资源以及存在
     *
     * @param message    消息
     * @param attachData 附带内容
     */
    public static void throwAlreadyExists(String message, Object attachData) {
        baseThrow(genBodyStruct(new Result(203, message, null, attachData)));
    }

    public static void throwAlreadyExists(String message) {
        throwAlreadyExists(message, null);
    }

    public static void throwAlreadyExists() {
        throwAlreadyExists("already exists.");
    }

    /**
     * 权限不足
     *
     * @param message 消息
     */
    public static void throwPermissionDenied(String message) {
        baseThrow(genBodyStruct(new Result(403, message)));
    }

    /**
     * 权限不足
     */
    public static void throwPermissionDenied() {
        throwPermissionDenied(null);
    }

}
